<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\BaoxiaoModel;
use app\models\Department;
use app\models\Employee;
use app\models\Expend;
use app\models\ExpendUse;
use app\services\JsonService;
use app\services\UtilService as Util;
use app\validate\ExpendValidate;
use cmf\controller\AdminBaseController;
use cmf\lib\Upload;
use think\Db;
use think\Exception;
use think\exception\ValidateException;

class ExpendController extends AdminBaseController
{
    function index(){
        $expend_use_model   = new ExpendUse();
        $department_model   = new Department();
        $use_list           = $expend_use_model->getList();
        $department_list    = $department_model->getList();
        $this->assign('use_list',$use_list);
        $this->assign('department_list',$department_list);
        return $this->fetch();
    }

    public function data()
    {
        $page           = $this->request->param('page','1');
        $limit          = $this->request->param('limit','10');
        $department_id  = $this->request->param('department_id',0);
        $use_id         = $this->request->param('use_id',0);
        $time           = $this->request->param('time','');
        if(empty($time)){
            $start_time = 0;
            $end_time = 0;
        }else{
            $array      = explode(' - ',$time);
            $start_time = $array[0];
            $end_time   = $array[1];
        }

        $employee_id    = 0;//根据角色处理
        $model      = new Expend();
        $res        = $model->getList($page,$limit,$department_id,$use_id,$employee_id,$start_time,$end_time);
        $count      = $model->getCount($department_id,$use_id,$employee_id,$start_time,$end_time);
        if($res){
            JsonService::successlayui($count,$res,'数据列表');
        }else{
            JsonService::fail(BaoxiaoModel::getErrorInfo());
        }
    }

    //报销信息
    public function operate()
    {
        $expend_id          = $this->request->param('expend_id');
        $expend_model       = new Expend();
        $expend_use_model   = new ExpendUse();
        $department_model   = new Department();
        $use_list           = $expend_use_model->getList();
        $department_list    = $department_model->getList();
        $this->assign('use_list',$use_list);
        $this->assign('department_list',$department_list);
        if(!empty($expend_id)){
            $expend = $expend_model->alias('ex')
                ->leftJoin('sh_employee em','ex.employee_id=em.id')
                ->where('ex.id',$expend_id)
                ->field('ex.*,em.name as employee')
                ->find();
            //支出图片
            if(!empty($expend['img'])){
                $expend['img'] = explode(',',$expend['img']);
            }
            $this->assign('expend',$expend);
            return $this->fetch('expend');
        }else{
            return $this->fetch('add');
        }



    }

    //数据提交
    public function do_operate()
    {
        try {
            $param      = $this->request->param();
            $validate   = validate(ExpendValidate::class);
            if(!$validate->check($param)){
                JsonService::fail($validate->getError());
            }
            $data = Util::postMore([
                ['id', 0],
                'money',
                'use_id',
                'department_id',
                'employee_id',
                'expend_time',
                ['img', ''],
                ['remark', ''],
            ]);
            $time = time();
            if(!empty($data['img'])){
                $data['img'] = implode(',',$data['img']);
            }
            $data['add_time']       = $time;
            $expend_model = new Expend();
            if($data['id']){
                $re = $expend_model->update($data);
            }else{
                unset($data['id']);
                $re = $expend_model->insert($data);
            }
            if($re){
                JsonService::success('操作成功');
            }else{
                JsonService::fail('操作失败');
            }
        }catch (Exception $exception){
            JsonService::fail($exception->getMessage());
        }
    }


    public function upload()
    {
        $uploader   = new Upload();
        $result     = $uploader->upload();
        if ($result === false) {
            JsonService::fail($uploader->getError());
        } else {
            JsonService::success("上传成功!",$result);
        }
    }

    public function get_img()
    {
        $id     = $this->request->param('id');
        $expend = new Expend();
        $img    = $expend->where('id',$id)->value('img');
        if(!$img){
            JsonService::fail('没有上传图片!');
        }
        $img_array = explode(',',$img);
        $data = [];
        foreach ($img_array as $k=>$v){
            $image['title'] = '图片';
            $image['src'] = $v;
            $image['thumb'] = $v;
            $data[] = $image;
        }
        $return['title']    = '图片';
        $return['id']       = '123';
        $return['data']     = $data;
        return json($return);
    }

}
